package com.dreamwhale.accelerator.configure;


import com.dreamwhale.accelerator.entity.AlManage;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * <p>Title: SessionInterceptor.java</p>
 * <p>Description: 拦截器配置</p>
 *
 * @author qcg
 * @date 2019年3月6日 下午2:46:40
 */
public class SessionInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
      /*  // 跨域操作
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Headers", "X-Requested-With,content-type,token");
        response.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH");
        response.setHeader("Content-type", "application/x-www-form-urlencoded");*/
        String us= request.getRequestURL().toString();
        us= us.substring(us.length()-1);
        if(us.equals("/")){
            response.sendRedirect("/backstage/404");
            return false;
        }
        if(us.equals("/user")){
            return true;
        }
        // 验证是否在操作后台
        if (request.getRequestURL().indexOf("/backstage") != -1) {
            // 登陆页面和404页面不做拦截
            if (request.getRequestURL().indexOf("/backstage/selectAlManage") != -1
                    ||request.getRequestURL().indexOf("/backstage/login") != -1) {
                return true;
            }
            // 验证用户是否存在
            AlManage object= (AlManage) request.getSession().getAttribute("user");
            if (object == null) {
                response.sendRedirect(request.getRequestURL()+"/backstage/login");
                return false;
            }
        }
/*        // 跨域操作
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST/GET");*/
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {

    }
}
